go to previous page   go to home page   go to next page

Answer:

sliderV.setPreferredSize( new Dimension( 50, 300) ); 

ChangeListener

Bouncing Change

As the user drags the knob across a slider, change events are generated. The number of events depends on how fast the user drags the knob. Dragging slowly generates an event every time the knob crosses an integer. An event listener must potentially deal with very many events.

A listener for a slider implements the ChangeListener interface. This interface has a single method:

public void stateChanged( ChangeEvent evt )

An object that implements the interface must be registered with the slider in order to receive events:

slider.addChangeListener( ChangeListener lstn )

Often, especially in small projects, the frame that contains the slider is also its listener.


QUESTION 8:

Say that you have a frame class that contains a slider and implements ChangeListener. Register an object of that class with its slider:

sliderV = new JSlider( JSlider.VERTICAL,  0, 1000, 400);
sliderV.setMajorTickSpacing( 100 );
sliderV.setMinorTickSpacing(  50 );
sliderV.setPaintTicks ( true );
sliderV.setPaintLabels( true ); 

sliderV.addChangeListener(  ); 

Hint: this is the same as we have been doing with frames that implement ActionListener and their buttons.